package com.tycmc.signelectroniccontracts.ui;

import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.images.Bimp;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;

import java.io.IOException;


/**
 * Created by gy on 2017/11/7.
 */

public class PopQianMing extends PopupWindow {


    private LinePathView linePathView;

    public PopQianMing(final ActBase context, final QianMingInterface linster) {
        super(context);
        View inflate = ((LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_draw_line, null);
        inflate.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        linePathView = ((LinePathView) inflate.findViewById(R.id.line_path_view));
        inflate.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (linePathView.getTouched()) {
                    PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsTool.PerMissionCallBack() {
                        @Override
                        public void permissIsPass(String[] permission, boolean isPass) {
                            if (isPass) {
                                try {
                                    String path = UrlPath.imagePath + "img_" + System.currentTimeMillis() + ".png";
                                    linePathView.save1(path);
                                    if (linster != null) {
                                        linster.qianMing(linePathView.getCachebBitmap(), path);
                                        dismiss();
                                    }
                                } catch (IOException e) {
                                    ToastShow.popupToast(context.getString(R.string.save_failed));
                                    e.printStackTrace();
                                }
                            } else {
                                ToastShow.popupToast(context.getString(R.string.write_use));
                            }
                        }
                    }, context);


                } else {
                    ToastShow.popupToast(context, "请签名");
                }
            }
        });
        inflate.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                linePathView.clear();
            }
        });
        this.setContentView(inflate);
        // 设置SelectPicPopupWindow弹出窗体的宽
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int screesnWidth = wm.getDefaultDisplay().getWidth();
        int screesnHeight = wm.getDefaultDisplay().getHeight();
        this.setWidth(screesnWidth);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_add_ainm);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public interface QianMingInterface {
        void qianMing(Bitmap bitmap, String path);
    }

    public void show(View view) {
        showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
